﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_PersonJobForm
 * 类型：       控制层
 * 功能说明：   职位信息编辑页面
 * 作者：       黄鑫
 * 创建日期：   2007/10/30
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.System.Validator;
using Shpm.System.Web.Control;
using log4net;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
public partial class App_Input_PersonJobForm : System.Web.UI.Page
{
    private const string MANAGER = "1";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            this.ViewState["USER_ID"] = loginId;

            if (Context.Items["JOB_ID"] != null)
            {
                ViewState["JOB_ID"] = Context.Items["JOB_ID"];
            }
            else
            {
                ViewState["JOB_ID"] = null;
            }

            if (Context.Items.Contains("ParamPageIndex"))
            {
                this.ViewState["ParamPageIndex"] = Context.Items["ParamPageIndex"];
            }
            if (Context.Items.Contains("CMB_GROUP_ID"))
            {
                this.ViewState["CMB_GROUP_ID"] = Context.Items["CMB_GROUP_ID"];
            }
            if (Context.Items.Contains("CMB_JOB_ID"))
            {
                this.ViewState["CMB_JOB_ID"] = Context.Items["CMB_JOB_ID"];
            }
            initPageControls();
        }
    }

    // bind common catalog data 
    private void initPageControls()
    {
        PersonJobDao dao = new PersonJobDao();
        // 人事审批权
        CommonDictionaryBean beanComDict = new CommonDictionaryBean();
        IList lstCat = null;
        beanComDict.Catalog = "M_JOB.JOB_LIMIT";
        lstCat = dao.FindByCatalog(beanComDict);
        cmbLimit.Items.Clear();
        if(lstCat.Count > 0)
        {
            foreach (CommonDictionaryBean bean in lstCat)
            {
                cmbLimit.Items.Add(new ListItem(bean.Content,bean.DictNo));
            }
        }

        //所属部门
        PersonJobBean beanGroup = new PersonJobBean();
        IList lstGroup = dao.ListGroup(beanGroup);
        cmbGroup.Items.Clear();
        if (lstGroup.Count > 0)
        {
            foreach (PersonJobBean bean in lstGroup)
            {
                cmbGroup.Items.Add(new ListItem(bean.DepartName, bean.DepartId));
            }
        }

        //直属上级
        PersonJobBean beanSuperior = new PersonJobBean();
        if(this.ViewState["JOB_ID"] != null)
        {
            beanSuperior.JobId = Convert.ToInt32(this.ViewState["JOB_ID"]);
            beanSuperior = dao.Find(beanSuperior);
        }
        PersonJobBean beanParam = new PersonJobBean();
        beanParam.JobLevel = beanSuperior.JobLevel;
        IList lstSuperior = dao.ListJob(beanParam);
        cmbSuperior.Items.Clear();
        if (lstSuperior.Count > 0)
        {
            foreach (PersonJobBean bean in lstSuperior)
            {
                cmbSuperior.Items.Add(new ListItem(bean.JobName, Convert.ToString(bean.JobId)));
            }
        }
        else
        {
            cmbSuperior.Items.Add(new ListItem("董事会",""));
        }


    }

    void Page_PreRender(object sender, EventArgs e)
    {
        if (this.ViewState["JOB_ID"] != null)
        {
            PersonJobBean bean = new PersonJobBean();
            PersonJobDao Dao = new PersonJobDao();
            bean.JobId = Convert.ToInt32(this.ViewState["JOB_ID"]);
            bean = Dao.Find(bean);
            txtName.Text = bean.JobName;
            cmbGroup.SelectedValue = bean.DepartId;
            cmbSuperior.SelectedValue = Convert.ToString(bean.SuperiorId);
            txtCorp.Text = bean.JobCorp;
            txtUnderline.Text = bean.JobUnderLine;
            txtQuantity.Text = Convert.ToString(bean.JobQuantity);
            cmbLimit.SelectedValue = bean.JobLimit;
            txtDescription.Text = bean.JobDescription;
            txtEduction.Text = bean.JobEducation;
            txtSkills.Text = bean.JobSkills;
            txtSuzhi.Text = bean.JobSuzhi;
            txtRelationInside.Text = bean.JobRelationInside;
            txtRelationOutSide.Text = bean.JobRelationOutside;
            this.ViewState["JOB_LEVEL"] = bean.JobLevel;
            this.ViewState["JOB_LEVEL_SERIAL"] = Convert.ToString(bean.JobLevelSerial);
            if(bean.JobLevel == MANAGER)
            {
                this.ViewState["SUPERIOR_ID"] = null;
            }
            else
            {
                this.ViewState["SUPERIOR_ID"] = Convert.ToString(bean.SuperiorId);
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        // do validate check
        ComValidateChecker vd = new ComValidateChecker();
        vd.CheckItems.Add(new CheckRequired(txtQuantity.Text, new ComMessage("E0094", "职位数")));
        vd.CheckItems.Add(new CheckLong(txtQuantity.Text, new ComMessage("E0322", "职位数")));

        if (!vd.DoValidator())
        {
            ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
            return;
        }

        PersonJobBean dataBean = new PersonJobBean();
        PersonJobDao Dao = new PersonJobDao();
        
        dataBean.GroupId = cmbGroup.SelectedValue;
        dataBean.JobName = txtName.Text;
        dataBean.JobLevelSerial = 0;
        if (Convert.ToString(this.ViewState["JOB_LEVEL"]) == MANAGER)
        {
            dataBean.SuperiorId = null;
        }
        else
        {
            dataBean.SuperiorId = Convert.ToInt32(cmbSuperior.SelectedValue);
        }
        dataBean.JobLimit = cmbLimit.SelectedValue;
        dataBean.JobCorp = txtCorp.Text;
        dataBean.JobUnderLine = txtUnderline.Text;
        dataBean.JobQuantity = Convert.ToInt32(txtQuantity.Text);
        dataBean.JobDescription = txtDescription.Text;
        dataBean.JobEducation = txtEduction.Text;
        dataBean.JobSkills = txtSkills.Text;
        dataBean.JobSuzhi = txtSuzhi.Text;
        dataBean.JobRelationInside=txtRelationInside.Text;
        dataBean.JobRelationOutside=txtRelationOutSide.Text;
        dataBean.UserId = Convert.ToString(this.ViewState["USER_ID"]);

        // get superiorBean
        PersonJobBean superiorBean = new PersonJobBean();
        superiorBean.JobId = dataBean.SuperiorId;
        superiorBean = Dao.Find(superiorBean);
        int serial = Convert.ToInt32(superiorBean.JobLevelSerial) + 1;
        string level = superiorBean.JobLevel;
        superiorBean.KeyId = serial;
        superiorBean = Dao.GetKeyValue(superiorBean);
        level = level + superiorBean.KeyValue;
        dataBean.JobLevel = level;
        superiorBean.JobLevelSerial = serial;
        superiorBean.JobId = dataBean.SuperiorId;

        if (ViewState["JOB_ID"] != null)
        {
            dataBean.JobLevelSerial = Convert.ToInt32(this.ViewState["JOB_LEVEL_SERIAL"]);
            dataBean.JobId = Convert.ToInt32(ViewState["JOB_ID"]);
            if (dataBean.SuperiorId == Convert.ToInt32(this.ViewState["SUPERIOR_ID"]) || dataBean.JobLevel == MANAGER)
            {
                dataBean.JobLevel = Convert.ToString(this.ViewState["JOB_LEVEL"]);
                Dao.UpdatePersonJob(dataBean);
            }
            else
            {
                int keyLength = Convert.ToString(this.ViewState["JOB_LEVEL"]).Length + 1;
                dataBean.KeyValue = Convert.ToString(this.ViewState["JOB_LEVEL"]);
                dataBean.KeyLength = keyLength;
                Dao.UpdatePersonJob(dataBean,superiorBean);
            }
        }
        else
        {
            ViewState["JOB_ID"] = Convert.ToInt32(Dao.InsertPersonJob(dataBean, superiorBean));
        }
        ((Design_MasterPage)this.Master).InfoMessage =
                HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string directUrl = "~/App/Input/PersonJobSrhForm.aspx";
        Context.Items["ParamPageIndex"] = this.ViewState["ParamPageIndex"];
        Context.Items["CMB_GROUP_ID"] = Convert.ToString(this.ViewState["CMB_GROUP_ID"]);
        Context.Items["CMB_JOB_ID"] = Convert.ToString(this.ViewState["CMB_JOB_ID"]);
        ControllerUtils.Transfer(this.Context, directUrl, false);
    }
}
